
function TangledController() {
	this.w = new Array();
	this.a = new Array();

	this.add_window = function(w, rows, cols) {
		this.w[w.guid] = new Array();
		this.w[w.guid] = w;
	}

	this.auto_click = function (w, min, max, row, col) {
		var c = this;
		// using lexical scope.  does this work in ie?
		var f = function () {
			c.clicked_any_tile(w, row, col);
			c.auto_click(w, min, max, row, col);

		}
		var i = min + (Math.floor(Math.random() * (max - min)));
		window.setTimeout(f, i);
	}

	this.auto_clicker = function(controller, w) {
		controller.clicked_any_tile(w);
	}

	this.clicked_any_tile = function (w, row, col) {
		var r = row;
		var c = col;
		if (null == row) { 
			r = Math.floor(this.w[w].p.h * Math.random());
		}
		if (null == col) {
			c = Math.floor(this.w[w].p.w * Math.random());
		}
		this.clicked_tile(w, r, c);
	}

	this.clicked_tile = function(w, row, col) {
		//alert("clicked window " + w + " row " + row + " col " + col);
		// if we clicked an edge tile, move the view in that direction
		// and update it
		
		var h = 0;
		var v = 0;

		if (0 == row) { 
			v = -1; 
		} else if (this.w[w].p.h - 1 == row) {
			v = 1;
		}

		if (0 == col) { 
			h = -1; 
		} else if (this.w[w].p.w - 1 == col) {
			h = 1;
		}

		if (0 != v || 0 != h) {
			this.w[w].move(v, h);
			this.w[w].update();
		} else {
			this.w[w].trace_tile(row, col);
			this.w[w].update();
		}
	}
}
